//
//  ViewController.swift
//  Community
//
//  Created by Vampire on 2019/7/18.
//  Copyright © 2019 ZKJ. All rights reserved.
//

import UIKit

class SWPTMainViewController: BaseViewController {

    let headImage = UIImageView()
    let name = UILabel()
    
    override func viewWillAppear(_ animated: Bool) {
        super .viewWillAppear(animated)
        
        if KIDUserModel.isLogin() {
            self.name.text = KIDUserModel.currentUserCount()

        }else{
           self.name.text = NSLocalizedString("登录/注册", comment: "")
        }
        self.tableView.reloadData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.fd_prefersNavigationBarHidden = true
        
        let topImageView = UIImageView.init(frame: CGRect (x: 0, y: 0, width: SCREEN_WIDTH, height: 300))
//        topImageView.image = UIImage.init(named: "bgC.jpg")
        topImageView.backgroundColor = mainGreenColor
        topImageView.isUserInteractionEnabled = true
        self.view.addSubview(topImageView)
        
        self.headImage.frame = CGRect (x: (SCREEN_WIDTH - 80)/2.0, y: (topImageView.height - 80)/2.0, width: 80, height: 80)
        self.headImage.image = UIImage.init(named: "user")
        self.headImage.backgroundColor = UIColor.white
        self.headImage.layer.cornerRadius = 40
        self.headImage.clipsToBounds = true
        self.headImage.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init { [weak self]  (sender) in
            self?.login()
        }
        self.headImage.addGestureRecognizer(tap)
        topImageView.addSubview(self.headImage)
        
        self.name.frame = CGRect (x: 0, y: self.headImage.n_maxY + 10, width: SCREEN_WIDTH, height: 30)
        self.name.font = constant.kFontSize(x: 20)
        self.name.textColor = UIColor.white
        self.name.textAlignment = NSTextAlignment.center
        
        topImageView.addSubview(self.name)
        self.tableView.frame = CGRect (x: 0, y: topImageView.n_maxY, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - topImageView.height - TabBarHeight)
        
        self.view.addSubview(self.tableView)
    }
    
    func login() {
        self.navigationController?.pushViewController(SEPTloginViewController(), animated: true)
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        
        if KIDUserModel.isLogin() {
            return 3
        }
        
        return  2
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        
        if cell == nil {
            cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
        }
        cell?.selectionStyle = UITableViewCell.SelectionStyle.none
        if indexPath.row == 0 {
            cell?.imageView?.image = UIImage.init(named: "collection")
            cell?.textLabel?.text = NSLocalizedString("查过的垃圾", comment: "")
        }else if indexPath.row == 1{
            cell?.imageView?.image = UIImage.init(named: "weather_icon")
            cell?.textLabel?.text = NSLocalizedString("看看天气", comment: "")
        }else{
            
            cell?.imageView?.image = UIImage.init(named: "exit_52.418410041841px_1226768_easyicon.net")
            cell?.textLabel?.text = "退出"
        }
        return cell!
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            //收藏
            
        }else if indexPath.row == 1{
            self.navigationController?.pushViewController(weatherViewController(), animated: true)
        }else{
            
           KIDUserModel.deleteAllObject()
            self.name.text = NSLocalizedString("登录/注册", comment: "")
            self.tableView.reloadData()
        }
    }
}

